import router from '@/router'

const useSettingStore = defineStore('setting',
  {
    state: ()=>({
      isScale: true,//是否进行全局适配
      echartsAutoTime: 3000,//echarts 图自动请求接口时间
      listAutoTime: 3000,//echarts 图自动请求接口时间
      currentIndex: 0,
      currentPageType: 1,
      settingShow: false,
      flushDevice: false,
      settingActiveName: 0,
      scaleIds: ['1','3','5','7','9','11','15'],
      sievesIds: ['17','19','21','23','25','27'],
      statisticsIds: ['contrast','briefing'],
    }),
    actions:{
      setIsScale(flag){
        this.isScale = flag
        this.setSettingData()
      },
      setSettingData ()  {
        localStorage.setItem('BigScreen-settingData', JSON.stringify(this.isScale))
      },
      setSettingShow(flag:boolean){
        this.settingShow = flag
      },
      setFlushDevice(flag:boolean){
        this.flushDevice = flag
      },
      setSettingActiveName(activeName:number){
        this.settingActiveName = activeName
      },
      initSetting(){
        let settingData:string = localStorage.getItem('BigScreen-settingData')!
        if (settingData) {
          let isScale:boolean = JSON.parse(settingData)
          this.setIsScale(isScale)
        }
      },
      setCurrentIndex(param:string,type:number){
        this.currentPageType = type
        let data:Array<string> = [];
        if (this.currentPageType === 1){
          data = this.scaleIds;
        } else if (this.currentPageType === 2) {
          data = this.sievesIds;
        } else if (this.currentPageType === 3) {
          data = this.statisticsIds;
        }
        if (data.length > 0){
          for (let item in data){
            if (param === data[item]){
              this.currentIndex = Number(item) + 1
            }
          }
        } else {
          this.currentIndex = 0
        }
      },
      changeCurrentIndex(value){
        let data:Array<string> = [];
        let routePath = ''
        if (this.currentPageType === 1){
          data = this.scaleIds;
          routePath = '/scale/'
        } else if(this.currentPageType === 2) {
          data = this.sievesIds;
          routePath = '/sieves/'
        }else if(this.currentPageType === 3) {
          data = this.statisticsIds;
          routePath = '/statistics/'
        }
        let routerParam = ''
        if (value === 1) {
          if (this.currentIndex > 1){
            this.currentIndex --
            routerParam = data[this.currentIndex-1]
          } else {
            routerParam = data[data.length-1]
          }
        }
        if (value === 2) {
          if (this.currentIndex < data.length){
            this.currentIndex++
            routerParam = data[this.currentIndex-1]
          } else {
            this.currentIndex = 1
            routerParam = data[0]
          }
        }
        router.push({ path: routePath + routerParam })
      }
    }
  })

export default useSettingStore